// miniprogram/pages/Address/Address.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    xuanzhong:"https://m.qpic.cn/psc?/V52Nm1aL3xiZRi2CArQp4LEE9y2vjS9x/ruAMsa53pVQWN7FLK88i5j.F1ZNQtTdZGgI*6xKVu0VIFJHXlZzobkuZ4Q5AJ5vw5XOpS*4fc3nQ.gfMqOCiCz93D144AFZ2JKXEiUfMWGk!/mnull&bo=LAAsAAAAAAADByI!&rf=photolist&t=5",
    weixuanzhong:"https://m.qpic.cn/psc?/V52Nm1aL3xiZRi2CArQp4LEE9y2vjS9x/ruAMsa53pVQWN7FLK88i5iZY83vyci4uLABWxTHHg0E2QRRCagkhEMYLb.HbbDi.ZBlyxBVzew9sKufx7vxOQu4toDDsUZNf9bl9EiQFbZY!/mnull&bo=LAAsAAAAAAADByI!&rf=photolist&t=5",
    addressList:[]

  },
  comeblack(){
    wx.navigateBack({
    })
  },
  tiaozhuannew(){
    wx.navigateTo({
      url: '../new Address/new Address',
    })
  },
  changzhuangtai(e){
    console.log(e)
    let index = e.currentTarget.dataset.index
    let address = this.data.addressList[index]
    wx.put('http://localhost:3000/wx/address/default',{
      addressId:address._id,
      memberId:wx.userInfo._id,
      isDefault:address.isDefault ==1 ? 0 :1
    }).then(res =>{
      if(!res.errorMsg){
        this.getAddressList()
      }
    })
  },
  getAddressList(){
    wx.get('http://localhost:3000/wx/address').then(res=>{
     this.setData({
      addressList:res
     })
    })
  },
  xiugaidizi(e){//点击修改地址 跳转到新增地址
    let address = this.data.addressList[e.currentTarget.dataset.index]
    wx.navigateTo({
      url: '../new Address/new Address?addressId=' + address._id
    })
  },
  shanchuaddress(e){//删除地址
    wx.showModal({
      title:'删除提示',
      content:'确定删除地址？',
      success:res=>{
        if(res.confirm){
          wx.delete('http://localhost:3000/wx/address',{addressId:e.currentTarget.dataset.index}).then(res =>{
            if(!res.errorMsg){
              this.getAddressList()
            }
          })
        }
      }
    }) 
  },
  /**
   * 
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAddressList()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAddressList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})